package com.hbnu.exercise.mysql;

import javax.swing.plaf.nimbus.State;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.ResourceBundle;

public class DatabaseDemo02
{
    public static void main(String[] args)
    {
        // 获取配置文件中的数据库信息
//        ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
//        String driverClass = resourceBundle.getString("driverClass");
//        String url = resourceBundle.getString("url");
//        String user = resourceBundle.getString("user");
//        String password = resourceBundle.getString("password");
        InputStream resourceAsStream = DatabaseDemo02.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try
        {
            properties.load(resourceAsStream);
            String driverClass = properties.getProperty("driverClass");
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");

            Class.forName(driverClass);

            Connection connection = DriverManager.getConnection(url, user, password);

            Statement statement = connection.createStatement();

            String sql = "DELETE FROM tb_user WHERE id = 1";

            int row = statement.executeUpdate(sql);
            System.out.println(row);

            statement.close();
            connection.close();
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }

    }
}
